/**
 * Created by root on 6/1/19.
 */
function getDataTitle( $){
    var type = $("input[name=type]").val();
    var dataTitle = {};
    switch ( type) {
        case "ENVIRONMENT":
            dataTitle.dataKey = "环境名称";
            dataTitle.dataValue = "环境地址";
            break;
        case "RECHARGE_TYPE":
            dataTitle.dataKey = "编号";
            dataTitle.dataValue = "收款方式";
            //收款方式，收款编号为只读
            $("#input[name=dataKey]").attr( "readonly", "readonly");
            break;
        case "CONSUMER_TYPE":
            dataTitle.dataKey = "编号";
            dataTitle.dataValue = "付款方式";
            //收款方式，收款编号为只读
            $("#input[name=dataKey]").attr( "readonly", "readonly");
            break;
        case "CUSTOMHOUSE":
            dataTitle.dataKey = "Catégorie";
            dataTitle.dataValue = "HS CODE";
            break;
        case "INTEGRAL_RATE":
            dataTitle.dataKey = "金额";
            dataTitle.dataValue = "积分转换率";
            break;
        default:
            dataTitle.dataKey = "数据名称";
            dataTitle.dataValue = "数据值";
            break;
    }
    $("#dataKeyName").html( dataTitle.dataKey);
    $("#dataValueName").html( dataTitle.dataValue);
}
layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        baseConfig = layui.baseConfig,
        $ = layui.jquery;
    getDataTitle( $);
    //绑定校验
    form.verify({
        dataKey : function(val){

        },
        dataValue : function(val){
            var type = $("input[name=type]").val();
            if( type == "INTEGRAL_RATE"){
                //配置积分转换率的时候，value必须是小数
                if( val > 1.00){
                    return "积分转换率必须小于1";
                }
            }
        }
    })

    //监听提交
    form.on("submit(submitBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //提交数据
        $.post("/dataDefine/addOrUpdate",{
            id : data.field.id,
            dataKey : data.field.dataKey,
            dataValue : data.field.dataValue,
            type : data.field.type
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });
});
